package cn.dnui_zzh807.dnuifood_16110100807.controller;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.List;

import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.adapter.FoodListAdapter;
import cn.dnui_zzh807.dnuifood_16110100807.bean.FoodInfo;
import cn.dnui_zzh807.dnuifood_16110100807.bean.UserCollectBean;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;
import cn.dnui_zzh807.dnuifood_16110100807.model.CollectModel;
import cn.dnui_zzh807.dnuifood_16110100807.model.FoodModel;
import cn.dnui_zzh807.dnuifood_16110100807.service.CollectService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodListActivity extends AppCompatActivity implements OnRetrofitListener<List<FoodInfo>> {
    private MenuItem menuItem;
    private Menu menu;
    private FoodInfo foodInfo;
    private RecyclerView recyclerView;
    private FoodListAdapter foodListAdapter;
    private BaseActivity baseActivity;
    private SharedPreferences sharedPreferences;
    private String shopname;
    public FoodListActivity(){

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences("zzh",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");

        String shop_id = getIntent().getStringExtra("shop_id");
        shopname = getIntent().getStringExtra("shopname");
        setContentView(R.layout.activity_food_list);
        Toast.makeText(FoodListActivity.this,user_id,Toast.LENGTH_SHORT).show();

        initViews();
        initData(shop_id);

    }

    //生成Menu的对象
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单
        getMenuInflater().inflate(R.menu.food_list_collect,menu);
        this.menu=menu;
        //菜单创建完毕后向服务器发送请求，判断是否已经收藏
        menu.findItem(R.id.item_collect1).setTitle(shopname);
        isCollected(menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //实现"显示收藏还是取消收藏"
        switch (item.getItemId()){
            case R.id.item_collect:
                String title = item.getTitle().toString();
                if (title.equals("收藏"))
                {
                    //收藏文章
                    collectShop(menu);
                }
                else
                {
                    //取消收藏
                    uncollectShop(menu);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    void initViews(){

        recyclerView = (RecyclerView)findViewById(R.id.food_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    void initData( String shop_id){
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodByShop(shop_id,this);//访问服务器请求
    }


    @Override
    public void onSuccess( List<FoodInfo> foodInfos, int flag) {
        foodListAdapter = new FoodListAdapter(this, foodInfos);
        recyclerView.setAdapter(foodListAdapter);
    }

    @Override
    public void onFail() {
        Toast.makeText(FoodListActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();

    }



    private void isCollected(Menu menu){
        String shop_id = getIntent().getStringExtra("shop_id");
        SharedPreferences sharedPreferences = getSharedPreferences("ljp",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectModel collectModel = new CollectModel();
        collectModel.isCollect(user_id,shop_id,"0",menu);
    }

    private void collectShop(Menu menu){
        String shop_id = getIntent().getStringExtra("shop_id");
        SharedPreferences sharedPreferences = getSharedPreferences("ljp",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectModel collectModel = new CollectModel();
        collectModel.userCollectShop(user_id,shop_id,menu);

    }

    private void uncollectShop(Menu menu){
        String shop_id = getIntent().getStringExtra("shop_id");
        SharedPreferences sharedPreferences = getSharedPreferences("ljp",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectModel collectModel = new CollectModel();
        collectModel.unCollectShop_01(user_id,shop_id,menu);
    }


    public void isCollect(String user_id, String shop_food_id, String flag){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<UserCollectBean> call = collectService.isCollected(user_id,shop_food_id,flag);
        Callback<UserCollectBean> callback = new Callback<UserCollectBean>() {
            @Override
            public void onResponse(Call<UserCollectBean> call, Response<UserCollectBean> response) {
                Toast.makeText(FoodListActivity.this,"12345678",Toast.LENGTH_SHORT).show();
                // listener.onSuccess(response.body(),5);
                if (response.body().getCollected().toString().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");
                    // Toast.makeText(context,"收藏成功",Toast.LENGTH_SHORT).show();
                }else {
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                    //Toast.makeText(context,"取消收藏成功",Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<UserCollectBean> call, Throwable t) {
                Toast.makeText(FoodListActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
                //listener.onFail();
            }
        };
        call.enqueue(callback);
    }
}
